package com.poker.duiued;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.core.content.ContextCompat;

import com.poker.jkiuus.R;
import com.poker.jkiuus.uyrfgd.AppManager;

public class SplashActivity extends Activity {

    private ProgressBar progressBar;
    private TextView tvAiRobot, tvWelcome;
    private String aiRobotText = "Chat Robot";
    private String welcomeText = "Welcome to this software";
    private int indexAiRobot = 0, indexWelcome = 0;
    private Handler textHandler = new Handler();
    private Runnable aiRobotRunnable, welcomeRunnable;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        tvAiRobot = findViewById(R.id.tvAiRobot);
        tvWelcome = findViewById(R.id.tvWelcome);

        progressBar = findViewById(R.id.progressBar);
        AppManager.addActivity(this);

        SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
        boolean hasAgreed = prefs.getBoolean("hasAgreed", false);

        if (!hasAgreed) {
            showAgreementDialog();
        }else {
            startMainActivity();
        }
    }

    private void startMainActivity() {
        aiRobotRunnable = new Runnable() {
            @Override
            public void run() {
                if (indexAiRobot < aiRobotText.length()) {
                    tvAiRobot.setText(aiRobotText.substring(0, ++indexAiRobot));
                    textHandler.postDelayed(this, 100);
                } else {

                    checkAndStartNextActivity();
                }
            }
        };

        // Welcome Text Animation
        welcomeRunnable = new Runnable() {
            @Override
            public void run() {
                if (indexWelcome < welcomeText.length()) {
                    tvWelcome.setText(welcomeText.substring(0, ++indexWelcome));
                    textHandler.postDelayed(this, 100);
                } else {

                    checkAndStartNextActivity();
                }
            }
        };

        textHandler.post(aiRobotRunnable);
        textHandler.post(welcomeRunnable);
    }

    private void checkAndStartNextActivity() {

        if (indexAiRobot >= aiRobotText.length() && indexWelcome >= welcomeText.length()) {
            progressBar.setVisibility(View.VISIBLE);
            new Handler().postDelayed(() -> {
                Intent intent = new Intent(SplashActivity.this, AiActivity.class);
                startActivity(intent);
                finish();
            }, 2000);
        }
    }

    private void showAgreementDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View customLayout = getLayoutInflater().inflate(R.layout.dialog_custom, null);
        builder.setView(customLayout);

        TextView tvMessage = customLayout.findViewById(R.id.tvMessage);

        final SpannableString message = new SpannableString("We are committed to taking industry-leading security measures to protect your information, and we will provide necessary services online when you use this app. \n\n" +
                "Please note that we only provide services to users over the age of 13, and if you are under the age of 13, please do not use our services in any way. When we learn that we have inadvertently collected personal information from a person under the age of 13, we will delete it or take other appropriate measures in accordance with applicable laws and regulations. \n\n" +
                "By clicking Agree, you are deemed to have read and agreed to 《Ai Robot's Terms and Conditions》, 《Privacy Policy》 and the above. ");


        setClickableSpan(message, "《Ai Robot's Terms and Conditions》", "https://sites.google.com/view/chat-robot-privacy", tvMessage);
        setClickableSpan(message, "《Privacy Policy》", "https://sites.google.com/view/chat-robot-privacy", tvMessage);

        tvMessage.setText(message);
        tvMessage.setMovementMethod(LinkMovementMethod.getInstance());
        tvMessage.setHighlightColor(Color.TRANSPARENT);

        AlertDialog dialog = builder.setPositiveButton("Agree", (dialog12, which) -> {
                    SharedPreferences.Editor editor = getSharedPreferences("prefs", MODE_PRIVATE).edit();
                    editor.putBoolean("hasAgreed", true);
                    editor.apply();
                    dialog12.dismiss();
                    startMainActivity();
                })
                .setNegativeButton("Disagree", (dialog1, which) -> finish())
                .setCancelable(false)
                .create();

        dialog.show();
    }

    private void setClickableSpan(SpannableString spannableString, String target, String url, TextView textView) {
        int start = spannableString.toString().indexOf(target);
        int end = start + target.length();
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(true);
                ds.setColor(ContextCompat.getColor(SplashActivity.this, R.color.blue));
            }
        }, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }


}

